package com.ojoin.trade.common_core.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.json.JSONUtils;
import com.ojoin.trade.common.web.controller.BaseController;
import com.ojoin.trade.common.web.controller.BaseControllerImpl;
import com.ojoin.trade.common.web.domain.Result;
import com.ojoin.trade.common.web.domain.SessionUser;
import com.ojoin.trade.common.web.service.BaseService;
import com.ojoin.trade.common_core.web.domain.TbMerchantInfo;
import com.ojoin.trade.common_core.web.domain.TbMerchantUser;
import com.ojoin.trade.common_core.web.service.MerchantUserBaseService;
import com.ojoin.trade.common_core.web.service.TbMerchantInfoBaseService;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 15-11-17
 * Time: 上午11:12
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/merchantinfo")
public class MerchantInfoController extends BaseControllerImpl<TbMerchantInfo> implements BaseController<TbMerchantInfo> {
	private Logger log = LoggerFactory.getLogger(MerchantInfoController.class);
    
	@Autowired
    private TbMerchantInfoBaseService tbMerchantInfoBaseService;

    @Override
    protected BaseService<TbMerchantInfo> getBaseService() {
        return tbMerchantInfoBaseService;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @ResponseBody
    @RequestMapping(value = "/SlRealName", method = RequestMethod.POST)
    public Result slRealName(HttpServletRequest request,TbMerchantInfo info){
        SessionUser uservo= CommonUtils.getSessionUser(request);
        if(uservo==null){
            return new Result(Result.Status.ERROR,"请重新登录");
        }
        int joinid=uservo.getId();
        info.setIdOfTbMerchantUser(joinid);
        try{
            ((TbMerchantInfoBaseService) getBaseService()).insert(info);
            return new Result(Result.Status.OK,"数据上传成功");
        }catch (Exception e){
            log.error("上传供应商实名信息失败",e);
                return new Result(Result.Status.ERROR,"数据上传失败");

        }

    }
    @ResponseBody
    @RequestMapping(value="/updateSlRealName",method=RequestMethod.POST)
    public Result updateSlRealName(HttpServletRequest request,TbMerchantInfo info){

        SessionUser uservo= CommonUtils.getSessionUser(request);
        if(uservo==null){
            return new Result(Result.Status.ERROR,"请重新登录");
        }
        try{
            ((TbMerchantInfoBaseService) getBaseService()).updateTbMerchentInfoByVo(info);
            return new Result(Result.Status.OK,"更新数据成功");
        }catch (Exception e){
            log.error("更新供应商实名信息失败",e);
            return new Result(Result.Status.ERROR,"更新数据失败");

        }
    }

    @ResponseBody
    @RequestMapping(value = "/getinfo",method = RequestMethod.GET)
    public Result getInfo(HttpServletRequest request) {
        SessionUser uservo=CommonUtils.getSessionUser(request);
        if(uservo==null){
            return new Result(Result.Status.ERROR,"请重新登录");
        }
        int joinid=uservo.getId();
        try{
            TbMerchantInfo info=  ((TbMerchantInfoBaseService) getBaseService()) .queryInfoByJoinId(joinid);
            return new Result(Result.Status.OK,info);
        }catch (Exception e){
            log.error("查询供应商的信息失败joinid"+joinid,e);
            return new Result(Result.Status.ERROR,"查询用户的信息失败");
        }
    }


    @Autowired
    private MerchantUserBaseService userService;
    
    @ResponseBody
    @RequestMapping(value = "/getInfoByJoinuserId", method = RequestMethod.GET)
    public Result getInfoByJoinuserId(HttpServletRequest request, @RequestParam int joinuserid){
        try{
        	TbMerchantUser uservo = userService.queryById(joinuserid);
        	List muserinfoList = userService.queryPayUserVOByID(uservo.getUserIdOfIPayNow());
        	if (CommonUtils.isSuccess(muserinfoList)) {
        		Map<String, String> data = (Map) muserinfoList.get(1);
        		String result = data.get("mhtData");
        		return new Result(Result.Status.OK, "joinuserid->" + joinuserid + ", mhtData->" + result);
        	} else {
        		return new Result(Result.Status.ERROR, "joinuserid->" + joinuserid + ", muserinfoList->" + muserinfoList.toString());
        	}
        }catch (Exception e){
            log.error("joinuserid->" + joinuserid + "查询实名信息失败",e);
            return new Result(Result.Status.ERROR,"joinuserid->" + joinuserid + "查询实名信息失败");
        }
    }
}
